package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CommodityCategoryService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.commoditycategory.vo.*;
import com.ibigcat.module.mdm.dao.entity.CommodityCategoryDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CommodityCategoryMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 商品分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommodityCategoryServiceImpl implements CommodityCategoryService {

    @Resource
    private CommodityCategoryMapper commodityCategoryMapper;

    @Override
    public Long createCommodityCategory(CommodityCategorySaveReqVO createReqVO) {
        // 插入
        CommodityCategoryDO commodityCategory = BeanUtils.toBean(createReqVO, CommodityCategoryDO.class);
        commodityCategoryMapper.insert(commodityCategory);
        // 返回
        return commodityCategory.getId();
    }

    @Override
    public void updateCommodityCategory(CommodityCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCommodityCategoryExists(updateReqVO.getId());
        // 更新
        CommodityCategoryDO updateObj = BeanUtils.toBean(updateReqVO, CommodityCategoryDO.class);
        commodityCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommodityCategory(Long id) {
        // 校验存在
        validateCommodityCategoryExists(id);
        // 删除
        commodityCategoryMapper.deleteById(id);
    }

    private void validateCommodityCategoryExists(Long id) {
        if (commodityCategoryMapper.selectById(id) == null) {
            throw exception(COMMODITY_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public CommodityCategoryDO getCommodityCategory(Long id) {
        return commodityCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<CommodityCategoryDO> getCommodityCategoryPage(CommodityCategoryPageReqVO pageReqVO) {
        return commodityCategoryMapper.selectPage(pageReqVO);
    }

}